<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\UserTable;
use App\Models\User as UserModel;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('avatar')->image(null, 30, 30);
            $grid->column('name');
            $grid->column('sex')->display(function ($sex) {
                return $sex == 1 ? '男' : '女';
            });
            $grid->column('phone');
            $grid->column('channel');
            $grid->column('province');
            $grid->column('city');
            $grid->column('referee_id');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->integer()->width(2);
                $filter->like('name')->width(2);
                $filter->equal('sex')->select([1 => '男', 2 => '女'])->width(2);
                $filter->equal('phone')->width(2);
                $filter->equal('channel')->width(2);
                $filter->equal('province')->width(2);
                $filter->equal('city')->width(2);
                $filter->between('created_at')->datetime()->width(4);
                $filter->equal('referee_id', '推荐官')->width(2)
                    ->selectTable(UserTable::make(['id' => ''])) // 设置渲染类实例，并传递自定义参数
                    ->title('用户列表')
                    ->dialogWidth('50%') // 弹窗宽度，默认 800px
                    ->model(UserModel::class, 'id', 'name'); // 设置编辑数据显示
            });
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->export()->csv();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('avatar')->image();
            $show->field('sex');
            $show->field('channel');
            $show->field('city');
            $show->field('province');
            $show->field('birthday');
            $show->field('last_actived_at');
            $show->field('phone');
            $show->field('weixin_openid');
            $show->field('weixin_unionid');
            $show->field('weapp_openid');
            $show->field('weixin_session_key');
            $show->field('remember_token');
            $show->field('created_at');
            $show->field('updated_at');
            $show->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableDelete();
                });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('avatar');
            $form->text('sex');
            $form->text('channel');
            $form->text('city');
            $form->text('province');
            $form->text('birthday');
            $form->text('last_actived_at');
            $form->text('phone');
            $form->text('weixin_openid');
            $form->text('weixin_unionid');
            $form->text('weapp_openid');
            $form->text('weixin_session_key');
            $form->text('remember_token');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function current($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('name', '昵称');
            $show->field('sex', '性别')->using([1 => '男', 2 => '女']);
            $show->field('city', '城市');
            $show->field('province', '省份');
            $show->field('birthday', '生日');
            $show->field('phone', '手机');
            $show->field('created_at', '创建时间');
            $show->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableDelete();
                    $tools->disableList();
                });
        });
    }
}
